// pages/set/index.js
import common from "../../common/compents.js";
import Toast from '@vant/weapp/toast/toast';
// 获取应用实例
const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseOrganize: "",
    account: "",
    mobile: "",
    accountType: "",
    show: false,
    value: "",
    allList: [],
    showList: [],
    loginshow: false,
    actions: [{
        name: "解除微信绑定",
        id: 0
      },
      {
        name: "退出登录",
        id: 1
      }
    ]
  },
  // 解除微信绑定
  closeBind() {
    let _token = wx.getStorageSync('token'),
      that = this;
    wx.request({
      url: App.globalData.testApi + '/wechat/passport/unbind',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "token": _token
      },
      data: {

      },
      success(resapi) {
        if (resapi.data.code == 0) {
          Toast("解绑成功！");
          wx.removeStorageSync('currentOrganize');
          wx.removeStorageSync('token');
          wx.removeStorageSync('initInfo');
          setTimeout(()=>{
            wx.reLaunch({
              url: '/pages/login/index'
            })
          },1000)
        }else if (resapi.data.code == 1008) {
          common.reLogin();
        } else {
          Toast(resapi.data.message)
        }
      },
      fail(resapi) {
        console.log('失败', resapi);
        Toast("网络请求失败！");
      },
      complete(resapi) {
        wx.hideLoading();
      }
    })
  },
  open(e) {
    // console.log(e);
    this.setData({
      show: true
    })
  },
  onSelect(e) {
    // console.log(e.detail)
    this.setData({
      loginshow: true
    })
    if (e.detail.id == 0) {
      wx.showLoading({
        mask: true
      })
      this.closeBind();
    } else if (e.detail.id == 1) {
      wx.removeStorageSync('currentOrganize');
      wx.removeStorageSync('token');
      wx.removeStorageSync('initInfo');
      Toast("退出登录成功")
      wx.reLaunch({
        url: '/pages/login/index'
      })
    }
  },
  openLogin(e) {
    this.setData({
      loginshow: true
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  onLoginClose(){
    this.setData({
      loginshow: false
    })
  },
  search(event) {
    let _val = event.detail.trim(),
      _arr = [];
    // console.log(_val)
    for (let i = 0; i < this.data.allList.length; i++) {
      console.log(this.data.allList[i].organization_name.indexOf(_val) > -1)
      if (this.data.allList[i].organization_name.indexOf(_val) > -1) {
        _arr.push(this.data.allList[i]);
      }
    }
    this.setData({
      showList: _arr,
    })
  },
  chooseOrg(e) {
    // console.log(e);
    let _obj = JSON.parse(wx.getStorageSync('initInfo')),
      choose, _str;
    for (let i = 0; i < this.data.allList.length; i++) {
      if (this.data.allList[i].organization_id == e.currentTarget.dataset.id) {
        choose = this.data.allList[i];
      }
    }
    wx.setStorageSync('currentOrganize', JSON.stringify(choose));
    if (_obj.type == 1) {
      _str = "平台级用户"
    } else {
      if (choose.user_organization_type == 1) {
        _str = "机构管理员"
      } else {
        _str = "普通用户"
      }
    }
    this.setData({
      chooseOrganize: choose.organization_name,
      value: "",
      show: false,
      showList: _obj.extra,
      accountType: _str
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // common.reLogin();
    if (wx.getStorageSync("token")) {
      let _obj = JSON.parse(wx.getStorageSync('initInfo')),
        currentOrganize = JSON.parse(wx.getStorageSync('currentOrganize')),
        _str = "",
        _id = 0;
      // console.log(_obj);
      if (_obj.type == 1) {
        _str = "平台级用户"
      } else {
        if (currentOrganize.user_organization_type == 1) {
          _str = "机构管理员"
        } else {
          _str = "普通用户"
        }
      }
      this.setData({
        mobile: _obj.mobile,
        account: _obj.account,
        chooseOrganize: currentOrganize.organization_name,
        allList: _obj.extra,
        accountType: _str,
        showList: _obj.extra
      })
    } else {
      wx.reLaunch({
        url: '/pages/login/index'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})